package com.glorysoft.lipuxinpda.recyevent;

import android.graphics.Color;
import android.util.Log;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.ItemTouchHelper;


import com.glorysoft.lipuxinpda.R;
import com.glorysoft.lipuxinpda.adapter.RecyclerViewAdapter;

import java.util.Collections;

/**
 * Description: RecyclerView-拖拽事件
 * Data：2019/6/24 14:27
 * Author YangT
 */
public class RecyItemTouchHelperCallback extends ItemTouchHelper.Callback {
    private final String TAG = RecyItemTouchHelperCallback.class.getSimpleName();
    RecyclerView.Adapter mAdapter;
    boolean isSwipeEnable;
    boolean isFirstDragUnable;
    //回调接口
    private static OnMoveListener mOnMoveListener;

    public RecyItemTouchHelperCallback(RecyclerView.Adapter adapter) {
        mAdapter = adapter;
        isSwipeEnable = true;
        isFirstDragUnable = false;
    }

    public RecyItemTouchHelperCallback(RecyclerView.Adapter adapter, boolean isSwipeEnable, boolean isFirstDragUnable) {
        mAdapter = adapter;
        this.isSwipeEnable = isSwipeEnable;
        this.isFirstDragUnable = isFirstDragUnable;
    }

    /**
     * 返回我们要监控的方向上下左右
     *
     * @param recyclerView
     * @param viewHolder
     * @return
     */
    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {
            int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
            int swipeFlags = 0;
            return makeMovementFlags(dragFlags, swipeFlags);
        } else {
            int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
            int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
            return makeMovementFlags(dragFlags, swipeFlags);
        }
    }

    /**
     * 滑动事件
     *
     * @param recyclerView
     * @param viewHolder   正在拖动item
     * @param target       要拖到的目标
     * @return
     */
    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        //得到当拖拽的viewHolder的Position
        int fromPosition = viewHolder.getAdapterPosition();
        //拿到当前拖拽到的item的viewHolder
        int toPosition = target.getAdapterPosition();
        //第一个不允许拖拽3
        //if (isFirstDragUnable && toPosition == 0) {
        //    return false;
        //}
        Log.e(TAG, TAG + "-->onMove:fromPosition=" + fromPosition + ",toPosition=" + toPosition);
        if (fromPosition < toPosition) {
            for (int i = fromPosition; i < toPosition; i++) {
                //itemView 移动了，对应的数据也要移动
                Collections.swap(((RecyclerViewAdapter) mAdapter).getDataList(), i, i + 1);
            }
        } else {
            for (int i = fromPosition; i > toPosition; i--) {
                //itemView 移动了，对应的数据也要移动
                Collections.swap(((RecyclerViewAdapter) mAdapter).getDataList(), i, i - 1);
            }
        }
        //列表fromPosition位置的数据移到toPosition位置时调用
        mAdapter.notifyItemMoved(fromPosition, toPosition);
        mOnMoveListener.onMoveDate(fromPosition, toPosition);
        return true;
    }

    /**
     * 侧滑删除（左右拖动的回调）
     *
     * @param viewHolder
     * @param direction
     */
    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        int adapterPosition = viewHolder.getAdapterPosition();
        mAdapter.notifyItemRemoved(adapterPosition);
        ((RecyclerViewAdapter) mAdapter).getDataList().remove(adapterPosition);
    }

    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
        if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
            viewHolder.itemView.setBackgroundColor(Color.LTGRAY);
        }
        super.onSelectedChanged(viewHolder, actionState);
    }

    @Override
    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        super.clearView(recyclerView, viewHolder);
        viewHolder.itemView.setBackgroundResource(R.color.transparent);
    }

    @Override
    public boolean isLongPressDragEnabled() {
        return !isFirstDragUnable;
    }

    @Override
    public boolean isItemViewSwipeEnabled() {
        return isSwipeEnable;
    }

    //回调接口
    public interface OnMoveListener {
        public void onMoveDate(int fromPosition, int toPosition);
    }

    //回调接口-方法
    public void setOnMoveListener(OnMoveListener mOnMoveListener) {
        this.mOnMoveListener = mOnMoveListener;
    }
}
